package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.login.anotation.NeedLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserCollectVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserInfoApiController {


    @Autowired
    private UserInfoService userInfoService;

    @GetMapping("/findUserSubscribePage/{pn}/{pz}")
    public Result findUserSubscribePage(@PathVariable(value = "pn") Long pn,
                                        @PathVariable(value = "pz") Long pz) {
        return Result.build(null, ResultCodeEnum.LOGIN_AUTH);
    }


    //Request URL: http://localhost:8500/api/user/userInfo/isCollect/51946
    /**
     * 根据音频id获取当前用户是否收藏了这条音频
     * @param trackId 音频id
     * @return 是否收藏
     */
    @GetMapping("/isCollect/{trackId}")
    @Operation(summary = "根据音频id获取当前音频是否被当前用户收藏")
    @NeedLogin
    public Result isCollect(@PathVariable Long trackId) {
        Boolean result = userInfoService.isCollect(trackId);
        return Result.ok(result);
    }


    //Request URL: http://localhost:8500/api/user/userInfo/collect/51946
    /**
     * 当前用户点击收藏按钮触发的接口
     * @param trackId 音频id
     * @return
     */
    @GetMapping("/collect/{trackId}")
    @Operation(summary = "当前用户点击收藏按钮触发的接口")
    @NeedLogin
    public Result collect(@PathVariable Long trackId) {
        Boolean result = userInfoService.collect(trackId);
        return Result.ok(result);
    }

    //Request URL: http://localhost:8500/api/user/userInfo/isSubscribe/1598

    /**
     * 根据当前用户id和专辑id判断当前用户是否订阅当前专辑
     * @param albumId 专辑id
     * @return 当前用户是否订阅专辑
     */
    @GetMapping("/isSubscribe/{albumId}")
    @Operation(summary = "根据专辑id获取当前用户是否订阅了这条专辑")
    @NeedLogin
    public Result isSubscribe(@PathVariable Long albumId) {
        Boolean result = userInfoService.isSubscribe(albumId);
        return Result.ok(result);
    }


    // Request URL: http://192.168.200.1:8500/api/user/userInfo/findUserCollectPage/1/10

    /**
     * 根据用户id展示用户分页收藏列表
     * @param pn pn
     * @param pz pz
     * @return 分页收藏列表
     */
    @NeedLogin
    @Operation(summary = "分页展示用户收藏列表")
    @GetMapping("/findUserCollectPage/{pn}/{pz}")
    public Result findUserCollectPage(@PathVariable(value = "pn") Long pn, @PathVariable(value = "pz") Long pz) {
        IPage<UserCollectVo> page = new Page<>(pn, pz);
        page = userInfoService.findUserCollectPage(page, AuthContextHolder.getUserId());
        return Result.ok(page);
    }
}

